var status = 0;
var strFullPath=window.document.location.href;
var strPath=window.document.location.pathname;
var pos=strFullPath.indexOf(strPath);
var prePath=strFullPath.substring(0,pos);
var project = window.location.pathname.split("/")[1];
var url = prePath+"/"+project+"/TeacherController/changeClassData.wm";
$(function () {
    $("#toptitle").html("建立管理班级");
    include_css(prePath+"/"+project+"/static/jquery-ui-bootstrap/css/custom-theme/jquery-ui-1.10.0.custom.css");
    include_js(prePath+"/"+project+"/static/bootstrap-3.3.5-dist/js/jquery.min.js");
    include_js(prePath+"/"+project+"/static/jquery-ui-bootstrap/js/jquery-ui-1.9.2.custom.min.js")
    $(".del").click(function () {
        var classId = $(this).parent().prev().prev().prev().prev().text();
        var tip = "del";
        if(confirm("确定要删除吗？")){
            $.post(url,{tip:tip,
            classId:classId},function (result) {
                if(result.msg=="yes"){
                    alert("删除成功！")
                    $("#tr"+classId).remove();
                }else{
                    alert("出现意外错误，删除失败！")
                }
            },"json")
        }
    });
    $(".king-btn-demo").click(function () {
        var tip = "add";
       var class_name = $("#add").val();
        var teacher_id = $("#hidden").val();
        var class_introduce = $("#textarea").val();
        var class_size = $("#sel option:selected").val();
        if(class_name==""||class_introduce==""){
            alert("文本不能为空！");
            return;
        }else{
            $.post(url,{tip:tip,
                class_name:class_name,
                class_introduce:class_introduce,
                teacher_id:teacher_id,
                class_size:class_size,
                class_name:class_name},function (result) {
                if(result.msg=="yes"){
                    alert("添加成功！");
                    window.location.reload();
                }else {
                    alert("系统错误！添加失败！")
                }
            },"json");
        }
    });
});
function include_js(path)
{
    var sobj = document.createElement('script');
    sobj.type = "text/javascript";
    sobj.src = path;
    var headobj = document.getElementsByTagName('head')[0];
    headobj.appendChild(sobj);
}
function include_css(path)
{
    var fileref=document.createElement("link");
    fileref.rel = "stylesheet";
    fileref.type = "text/css";
    fileref.href = path;
}
function saveClick() {
    $(".save").click(function () {
        var tip = "save";
        var newClassName =  $("#cn").val();
        var newClassSize = $("#spinner").val();
        var classNumber = $(this).parent().prev().prev().text();
        var classId = $(this).parent().prev().prev().prev().prev().text();
       // alert(classId+"classNumber"+classNumber+"newClassSize:"+newClassSize);
            var thisElm = $(this);
        if(parseInt(classNumber)>parseInt(newClassSize)){
            alert("当前班级人数已超过上限！请重新修改！");
        }else{
            $.post(url,{tip:tip,
                newClassName:newClassName,
                classId:classId,
                newClassSize:newClassSize},function (result) {
              //  alert("进入！");
               // alert(result.msg);
                if(result.msg=="yes") {
                    alert("修改成功！");
                    thisElm.parent().prev().prev().prev().html(newClassName);
                    thisElm.parent().prev().html(newClassSize);
                    thisElm.before("<a href='#' onclick='changeClass(this)'>【修改】</a>");
                    thisElm.remove();
                    status=0;
                }else{
                    alert("修改失败！")
                }
            },"json");
        }
        
    });
}
function changeClass(obj) {
    if(status==0){
        var classId = $(obj).parent().prev().prev().prev().prev().text();
        var className = $(obj).parent().prev().prev().prev().text();
        var classSize = $(obj).parent().prev().text();
        $(obj).parent().prev().prev().prev().html("<input type='text' value="+className+" id='cn'/>");
        $(obj).parent().prev().html("<input type='text' id='spinner'value="+classSize+">");
        $(obj).before("<a href='#' class='save'>【更新】</a>");
        status = 1;
        $(obj).remove();
        $("#spinner").spinner({
            max:120,
            min:0,
            step:1
        });
        saveClick();
    }else{
        alert("不能同时修改两个班级！");
    }
}
